package _18_剑指OfferII;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class _032_剑指OfferII有效的变位词 {

    // 包含unicode字符串
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length() || s.equals(t)) return false;

        Map<Character, Integer> containers = new HashMap<>();
        for(char s1: s.toCharArray()) {
            containers.put(s1, containers.getOrDefault(s1, 0) + 1);
        }

        for(char t1: t.toCharArray()) {
            if (!containers.containsKey(t1) || containers.get(t1) == 0) return false;

            containers.put(t1, containers.get(t1) - 1);
        }
        return true;
    }

    // 当为小写字母，可以使用数组替代hash表
    public boolean isAnagram1(String s, String t) {
        int sLen = s.length();
        int tLen = t.length();
        if (sLen != tLen || s.equals(t)) return false;
        int[] c1 = new int[26];
        int[] c2 = new int[26];
        for (int i = 0; i < sLen; i++) {
            c1[s.charAt(i) - 'a']++;
            c2[t.charAt(i) - 'a']++;
        }
        return Arrays.equals(c1, c2);
    }

}
